package com.junsi.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.junsi.bean.Jurisdiction;
import com.junsi.bean.Staff;
import com.junsi.service.JurisdictionSerivce;
import com.junsi.service.StaffSerivce;
import com.junsi.util.InteractiveUtil;

@Controller
@RequestMapping("/login")
public class LoginController {
	
	@Autowired
	private StaffSerivce staffSerivce;
	
	@Autowired
	private JurisdictionSerivce jurisdictionSerivce;
	
	@RequestMapping("/index")
	public ModelAndView index(Staff staff,HttpServletRequest request){
		ModelAndView modelAndView = new ModelAndView();
		if(staff != null){
			if(staff.getStaff_userName() != null && !"".equals(staff.getStaff_userName())){
				if(staff.getStaff_password() != null && !"".equals(staff.getStaff_password())){
					Staff staff2 = staffSerivce.findByUserName(staff.getStaff_userName());
					if(staff2 != null){
						if(staff2.getStaff_password().equals(staff.getStaff_password())){
							HttpSession session = request.getSession();
							session.setAttribute("staff", staff2);
							Jurisdiction jurisdiction = jurisdictionSerivce.findByStaff(staff2.getStaff_id());
							if(jurisdiction != null && jurisdiction.getJurisdiction_name() != null){
								String name = jurisdiction.getJurisdiction_name();
								if(name.equals("本人")){
									String strId = String.valueOf(staff2.getStaff_id());
									session.setAttribute("staffs",strId );
								}else if(name.equals("本组")){
									List<Staff> staffs = staffSerivce.findByDepartment(staff2.getStaff_departmentId(), 1000, 0);
									String strId = "";
									if(staffs.size()>0){
										for (Staff staff3 : staffs) {
											String str = String.valueOf(staff3.getStaff_id());
											strId +=""+str+",";
										}
									}
									strId += ""+staff2.getStaff_id()+",";
									strId = strId.substring(0, strId.length()-1);
									
									session.setAttribute("group", 1);
									session.setAttribute("staffs", strId);
								}else if(name.equals("本部门")){
									List<Staff> staffs = staffSerivce.findByDepartment(staff2.getStaff_departmentId(), 1000, 0);
									List<Staff> list = staffSerivce.findByDepartmentId(staff2.getStaff_departmentId());
									
									String strId = "";
									for (Staff staff3 : staffs) {
										String str = String.valueOf(staff3.getStaff_id());
										strId +=""+str+",";
									}
									if(list.size()>0){
										for (Staff staff3 : list) {
											String str = String.valueOf(staff3.getStaff_id());
											strId +=""+str+",";
										}
									}
									strId = strId.substring(0, strId.length()-1);
									
									session.setAttribute("staffs", strId);
									session.setAttribute("role", "1");
								}else if(name.equals("管理员")){
									List<Staff> staffs = staffSerivce.findByAll();
									String strId = "";
									if(staffs.size()>0){
										for (Staff staff3 : staffs) {
											String str = String.valueOf(staff3.getStaff_id());
											strId +=""+str+",";
										}
									}
									strId += ""+staff2.getStaff_id()+",";
									strId = strId.substring(0, strId.length()-1);
									
									session.setAttribute("staffs", strId);
									session.setAttribute("backstage", 1);
									session.setAttribute("role", "1");
									modelAndView.setViewName("redirect:/staff/selectTree.action");
									return modelAndView;
								}else{
									String strId = String.valueOf(staff2.getStaff_id());
									session.setAttribute("staffs",strId );
								}
							}
							else{
								//如果没有权限只能查看自己所负责的
								String strId = String.valueOf(staff2.getStaff_id());
								session.setAttribute("staffs",strId );
							}
							modelAndView.setViewName("/notice/findByAll.action");
							
						}else{
							modelAndView.setViewName("forward:/html/login.html");
						}
					}else{
						modelAndView.setViewName("forward:/html/login.html");
					}
				}else{
					modelAndView.setViewName("forward:/html/login.html");
				}
			}else{
				modelAndView.setViewName("forward:/html/login.html");
			}
		}else{
			modelAndView.setViewName("forward:/html/login.html");
		}
		
		return modelAndView;
		
	}
	
	/**
	 * 退出session
	 * @param id
	 * @param response
	 */
	@RequestMapping(value="tuichustaff")
	public ModelAndView tuichustaff(HttpServletRequest request,HttpServletResponse response){
		ModelAndView modelAndView =new ModelAndView();
		request.getSession().invalidate();
		modelAndView.setViewName("forward:../html/login.html");
		return modelAndView;
	}
	
}
